package com.hbase.zbf.thase;

import com.alibaba.fastjson.JSON;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author: LCG
 * @date: 2022-04-29 14:04:56
 * @description: HBASE的JavaApi测试
 **/
@Component
public class TestHabse {

    //创建一个 namespace
    public void namespace() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();
        NamespaceDescriptor mynamespace = NamespaceDescriptor.create("mynamespace").build();
        admin.createNamespace(mynamespace);
        connection.close();
    }

    
    /** 
     * @author: LCG
     * @date: 2022-04-29 16:25:04
     * @description:  删除一个表空间
     */
    public void namespaceDelete() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();

        //删除一个命名空间
        //admin.deleteNamespace("mynamespace");

        //查看命名空间的列表
        String[] strings = admin.listNamespaces();
        System.out.println(JSON.toJSONString(strings));


        connection.close();
    }

    /** 
     * @author: LCG
     * @date: 2022-04-29 17:34:33
     * @description:  创建一个表
     */
    public void createTable() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);
        Admin admin = connection.getAdmin();

        //定义列组
        ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("myliezu1".getBytes()).setMaxVersions(3);
        ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();

        //查看表是不是存在
        //admin.tableExists(TableName.valueOf("zbfnamespace:table2"));

        //创建一个表
       /* TableDescriptor table2 = TableDescriptorBuilder.newBuilder(TableName.valueOf("zbfnamespace:table2"))
                .setColumnFamily(columnFamilyDescriptor)
                .build();*/

       // admin.createTable(table2);


        //修改一表的列组信息
        admin.modifyColumnFamily(TableName.valueOf("zbfnamespace:table2"),columnFamilyDescriptor);

        connection.close();
    }


    /** 
     * @author: LCG
     * @date: 2022-04-29 18:02:26
     * @description:  Hbase数据操作
     */
    public void dataOper() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection connection = ConnectionFactory.createConnection(conf);

        Table table = connection.getTable(TableName.valueOf("zbfnamespace:table2"));


        //获取并遍历表的名称
        /*admin.listTableDescriptors().stream().forEach(tableDescriptor -> {
            System.out.println(new String(tableDescriptor.getTableName().getName()));
        });*/

        //向表中添加数据
       /* Put put=new Put(Bytes.toBytes("R002"));
        put.addColumn(Bytes.toBytes("myliezu1"),Bytes.toBytes("userName"),Bytes.toBytes("李四02"));
        put.addColumn(Bytes.toBytes("myliezu1"),Bytes.toBytes("address"),Bytes.toBytes("北京市海淀区XXXX路口"));
        table.put(put);
        table.close();*/


        //查询表中的数据
        Get get=new Get(Bytes.toBytes("R001"));
        get.addFamily(Bytes.toBytes("myliezu1"));
        Result result = table.get(get);
        List<Cell> cells = result.listCells();
        String str=null;
        for (Cell cell : cells) {

            System.out.println(Bytes.toString(CellUtil.cloneQualifier(cell))+":"+Bytes.toString(CellUtil.cloneValue(cell)));

        }


    }


}
